package com.community.property.service.impl;

import java.util.List;

import com.community.common.utils.DateUtils;
import com.community.property.RedisConst.RedisConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import com.community.property.mapper.NoticeMapper;
import com.community.property.domain.Notice;
import com.community.property.service.INoticeService;

/**
 * 物业公告Service业务层处理
 *
 * @author ruoyi
 * @date 2025-10-27
 */
@Service
@Slf4j
public class NoticeServiceImpl implements INoticeService {
    @Autowired
    private NoticeMapper noticeMapper;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 查询物业公告
     *
     * @param noticeId 物业公告主键
     * @return 物业公告
     */
    @Override
    @Cacheable(value = RedisConst.PROPERTY_MANAGEMENT, key = "'notice:' + #noticeId", unless = "#result == null")
    public Notice selectNoticeByNoticeId(Long noticeId) {
        System.out.println("从数据库查询公告：" + noticeId); // 如果看到这个日志，说明缓存没命中
        return noticeMapper.selectNoticeByNoticeId(noticeId);
    }

    /**
     * 查询物业公告列表
     *
     * @param notice 物业公告
     * @return 物业公告
     */
    @Override
    @Cacheable(value = RedisConst.PROPERTY_MANAGEMENT,
            key = "'notice:list'", unless = "#result == null || #result.isEmpty()")
    public List<Notice> selectNoticeList(Notice notice) {
        log.info("缓存内容为：{}",notice);
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * n
     *
     * @param notice 物业公告
     * @return 结果
     */
    @Override
    @CacheEvict(value = RedisConst.PROPERTY_MANAGEMENT, allEntries = true)
    public int insertNotice(Notice notice) {
        notice.setCreateTime(DateUtils.getNowDate());
        return noticeMapper.insertNotice(notice);
    }

    /**
     * 修改物业公告
     *
     * @param notice 物业公告
     * @return 结果
     */
    @Override
    @CacheEvict(value = RedisConst.PROPERTY_MANAGEMENT, allEntries = true)
    public int updateNotice(Notice notice) {
        notice.setUpdateTime(DateUtils.getNowDate());
        return noticeMapper.updateNotice(notice);
    }

    /**
     * 批量删除物业公告
     *
     * @param noticeIds 需要删除的物业公告主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = RedisConst.PROPERTY_MANAGEMENT, allEntries = true)
    public int deleteNoticeByNoticeIds(Long[] noticeIds) {
        return noticeMapper.deleteNoticeByNoticeIds(noticeIds);
    }

    /**
     * 删除物业公告信息
     *
     * @param noticeId 物业公告主键
     * @return 结果
     */
    @Override
    @CacheEvict(value = RedisConst.PROPERTY_MANAGEMENT, allEntries = true)
    public int deleteNoticeByNoticeId(Long noticeId) {
        return noticeMapper.deleteNoticeByNoticeId(noticeId);
    }
}
